package br.com.rapidrest.demo.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class PhoneBook implements Serializable, Comparable<PhoneBook> {

	private static final long serialVersionUID = 5823897086867101445L;

	private Long id;

	private String name;

	private List<PhoneEntry> entries;

	public PhoneBook(String name) {
		super();
		this.name = name;
	}

	public PhoneBook() {
		super();
	}	

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(cascade = CascadeType.ALL)
	public List<PhoneEntry> getEntries() {
		return entries;
	}
	
	public void addEntry(PhoneEntry entry) {
		List<PhoneEntry> entries = getEntries();
		if (entries == null) {
			entries = new ArrayList<PhoneEntry>();
			setEntries(entries);
		}
		entries.add(entry);
	}

	public void setEntries(List<PhoneEntry> entries) {
		this.entries = entries;
	}

	@Override
	public int compareTo(PhoneBook o) {
		return this.name.compareTo(o.name);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PhoneBook other = (PhoneBook) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
